package guava.function;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import org.junit.Test;

import java.util.*;

import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.or;
import static com.google.common.collect.Collections2.filter;

/**
 * Created by Administrator on 2014/6/12.
 */
public class test {

    @Test
    public void cutSize (){
        List<String> names = new LinkedList<String>();
        names.add("Aleksander");
        names.add("Jaran");
        names.add("Integrasco");
        names.add("Guava");
        names.add("Java");
        Iterable<String> filtered = filter(names, or(or(equalTo("Aleksander"),equalTo("Jaran")), lengthLessThan(5)));
        Iterator<String> it = filtered.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }


    @Test
    public void sortCollections(){
        List persons=new ArrayList();
        persons.add(new Person("高","旭"));
        persons.add(new Person("a","b"));
        persons.add(new Person("张","三"));
        Comparator<Person> byLastName = new Comparator<Person>() {
            public int compare(final Person p1, final Person p2) {
                return p1.getLastName().compareTo(p2.getLastName());
            }
        };

        Comparator<Person> byFirstName = new Comparator<Person>() {
            public int compare(final Person p1, final Person p2) {
                return p1.getFirstName().compareTo(p2.getFirstName());
            }
        };

        //1
        List<Person> sortedCopy = Ordering.from(byLastName).compound(byFirstName).reverse().sortedCopy(persons);
        for(Person p:sortedCopy){
            System.out.println(p.getFirstName()+":"+p.getLastName());
        }
    }






    private static class LengthLessThanPredicate implements Predicate<String> {
        private final int length;
        private LengthLessThanPredicate(final int length) {
            this.length = length;
        }
        public boolean apply(final String s) {
            return s.length() < length;
        }
    }

    public static Predicate<String> lengthLessThan(final int length) {
        return new LengthLessThanPredicate(length);
    }
}
